package com.dyava.commons.validate;

import java.util.function.Consumer;

public class ValidateArray<T> {
	protected boolean relax;
	protected T[] values;
	protected String name;

	public ValidateArray(T[] values,String name){
		this.name = name;
		if(values!=null){
			this.values = values;
		}
	}

	private int _size(T[] v){
		return v.length;
	}

	private boolean _empty(T[] v){
		return (v==null||v.length==0);
	}

	protected boolean checkEmpty(){
		if(relax && _empty(values)){
			return true;
		}
		notEmpty();
		return false;
	}

	public ValidateArray<T> orEmpty(){
		relax = true;
		return  this;
	}

	public ValidateArray<T> notEmpty(){
		if(!_empty(this.values)){
			return this;
		}
		throw new ValidateFastException(name+"必须不为空" );
	}
	@Deprecated
	public ValidateArray<T> isNotEmpty() {
		return notEmpty();
	}
	public  ValidateArray<T> sizeEq(int size){
		if (checkEmpty()) return this;
		if(_size(this.values) == size ){
			return this;
		}
		throw new ValidateFastException(name+"数量必须等于"+size);
	}
	public  ValidateArray<T> sizeGt(int size){
		if (checkEmpty()) return this;
		if(_size(this.values) > size ){
			return this;
		}
		throw new ValidateFastException(name+"数量必须大于"+size);
	}
	public  ValidateArray<T> sizeGe(int size){
		if (checkEmpty()) return this;
		if(_size(this.values) >= size ){
			return this;
		}
		throw new ValidateFastException(name+"数量必须大于等于"+size);
	}
	public  ValidateArray<T> sizeLt(int size){
		if (checkEmpty()) return this;
		if(_size(this.values) < size ){
			return this;
		}
		throw new ValidateFastException(name+"数量必须小于"+size);
	}
	public  ValidateArray<T> sizeLe(int size){
		if (checkEmpty()) return this;
		if(_size(this.values) <= size ){
			return this;
		}
		throw new ValidateFastException(name+"数量必须小于等于"+size);
	}

	public  ValidateArray<T> forEach(Consumer<T> consumer){
		for(T t : values){
			consumer.accept(t);
		}
		return this;
	}

	public T[] values(){
		return values;
	}

	public T[] values(T[] values){
		if(this.values==null){
			return values;
		}
		return this.values;
	}

}
